package com.lhh.jiu.controller;

import com.lhh.jiu.data.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    //判断用户是否登录成功
    @RequestMapping("/toLogin")
    @ResponseBody
    public Object toLogin(String username,String password){
        if(username==null || password == null){
            return Result.getHashMapLogin("用户名和密码不存在","2");
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
        }catch (UnknownAccountException e) {
            return Result.getHashMapLogin("用户名不存在","2");
        }catch (IncorrectCredentialsException e){
            return Result.getHashMapLogin("密码错误","2");
        }
        return Result.getHashMapLogin("验证成功","1");
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
